<template>
  <el-form ref="formRef" :model="form" label-width="120px" size="default">
    <el-form-item>
      <h2>
        <el-link type="warning">将身份证号改为护照号以便出示离境报告</el-link>
      </h2>
    </el-form-item>
    <el-form-item label="开始时间：">
      <el-date-picker
        v-model="form.reviewTime"
        :editable="false"
        placeholder="选择日期时间"
        format="YYYY-MM-DD HH:mm:ss"
        value-format="YYYY-MM-DD HH:mm:ss"
      >
      </el-date-picker>
    </el-form-item>
    <el-form-item label="card_number：">
      <el-input v-model="form.cardNumber" clearable class="input-width"></el-input>
    </el-form-item>
    <el-form-item label="护照号：">
      <el-input v-model="form.checked_person_card_number" clearable class="input-width"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="danger" @click="reset">重置</el-button>
    </el-form-item>
    <el-form-item label="">
      {{ JSON.stringify(form.cardNumbers) }}
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="onSubmit">生成脚本</el-button>
      <el-button type="default" @click="exportFile">导出文件</el-button>
    </el-form-item>
    <el-form-item label="脚本：">
      <el-input autosize type="textarea" v-model="form.scriptString" readonly></el-input>
    </el-form-item>
  </el-form>
</template>

<script setup>
import { reactive } from "vue";
import saveAs from "./js/FileSaver";
// do not use same name with ref
const form = reactive({
  // 修改时间
  reviewTime: "",
  // 身份证号
  cardNumber: "",
  // 护照号
  checked_person_card_number: "",
  // 脚本
  scriptString: ""
});

const reset = () => {
  form.reviewTime = "";
  form.cardNumber = "";
  form.checked_person_card_number = "";
  form.scriptString = "";
};

const exportFile = () => {
  let str = new Blob([form.scriptString], {
    type: "text/plain;charset=utf-8"
  });
  saveAs(str, `离境 证件号修改为护照号脚本.txt`);
};

const onSubmit = () => {
  form.scriptString = `环境：太极政务云
数据库地址：192.159.107.33:2000
数据库名：nat_admin

涉及数据量：nat_appo_check 1条
nat_appo_check_relation 1条
olympic_register_card_person 1条
 
 
先备份：（请将备份的数据写在jira的备注里，如果数据多，可以生成txt文件上传到jira里）
var c = db.nat_appo_check.find({"checked_person_card_number":"${form.cardNumber}","sam_time":{"$gte":"${form.reviewTime}"}})
d = []
while(c.hasNext()) {
d.push(c.next())
}
printjson(d)

var c = db.nat_appo_check_relation.find({"checked_person_card_number":"${form.cardNumber}","sam_time":{"$gte":"${form.reviewTime}"}})
d = []
while(c.hasNext()) {
d.push(c.next())
}
printjson(d)

var c = db.olympic_register_card_person.find({"card_number":"${form.cardNumber}"})
d = []
while(c.hasNext()) {
d.push(c.next())
}
printjson(d)

再执行脚本
db.olympic_register_card_person.update({"card_number":"${form.cardNumber}"},{"$set":{"card_number":"${form.checked_person_card_number}"}});
db.nat_appo_check.update({"checked_person_card_number":"${form.cardNumber}","sam_time":{"$gte":"${form.reviewTime}"}},{"$set":{"checked_person_card_number":"${form.checked_person_card_number}"}},false,true);
db.nat_appo_check_relation.update({"checked_person_card_number":"${form.cardNumber}","sam_time":{"$gte":"${form.reviewTime}"}},{"$set":{"checked_person_card_number":"${form.checked_person_card_number}"}},false,true);`;
};
</script>

<style>
.input-width {
  width: 500px;
}
</style>
